﻿using System;
using System.ComponentModel;
using System.Windows.Input;
using GC_FFW.Common.Enums;
using GC_FFW.Common.Helpers;
using GC_FFW.ForecastWriterClient.Helpers;
using GC_FFW.ForecastWriterClient.ViewModels.Parameters;

namespace GC_FFW.ForecastWriterClient.ViewModels
{
	public class ImportDataManagerViewModel : INotifyPropertyChanged
	{
		#region Variables

		private readonly ImportDataManagerViewModelParameters _parameters;

		private bool _isWindowEnabled;
		private string _importFilePath;
		private string _statusMessage;

		private RelayCommand _browseCommand;
		private RelayCommand _importCommand;

		#endregion

		#region Properties

		public string ImportFilePath
		{
			get { return _importFilePath; }
			set
			{
				_importFilePath = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("ImportFilePath"));
			}
		}

		public bool IsWindowEnabled
		{
			get { return _isWindowEnabled; }
			set
			{
				_isWindowEnabled = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("IsWindowEnabled"));
			}
		}

		public string StatusMessage
		{
			get { return _statusMessage; }
			set
			{
				_statusMessage = value;
				if (PropertyChanged == null) return;
				PropertyChanged(this, new PropertyChangedEventArgs("StatusMessage"));
			}
		}

		public ICommand BrowseCommand
		{
			get { return _browseCommand; }
		}

		public ICommand ImportCommand
		{
			get { return _importCommand; }
		}

		#endregion

		#region Constructors

		public ImportDataManagerViewModel(ImportDataManagerViewModelParameters parameters)
		{
			if (parameters == null)
				throw new ArgumentNullException(string.Format("Parameters"));
			_parameters = parameters;

			if (_parameters.BrowserDialog == null)
				throw new ArgumentNullException(string.Format("BrowserDialog"));

			FillViewModel();
		}

		#endregion

		#region Internal Implementation

		private void FillViewModel()
		{
			_browseCommand = new RelayCommand(Browse);
			_importCommand = new RelayCommand(Import);

			_isWindowEnabled = true;
		}

		private void Browse(object parameter)
		{
			ImportFilePath = _parameters.BrowserDialog.ShowFileBrowserDialog(FileType.XML);
		}

		private void Import(object parameter)
		{
			IsWindowEnabled = false;
			StatusMessage = "Import Process...";

			var worker = new BackgroundWorker();
			worker.DoWork += Import_DoWork;
			worker.RunWorkerCompleted += Import_DoWorkComplete;
			worker.RunWorkerAsync();
		}

		private void Import_DoWork(object sender, DoWorkEventArgs e)
		{
			_parameters.ImportService.ImportNames(ImportFilePath, _parameters.ImportDataType);
		}

		private void Import_DoWorkComplete(object sender, RunWorkerCompletedEventArgs e)
		{
			if (e.Error != null)
			{
				ApplicationLogger.Logger.LoggingExceptionMessage(e.Error);

				StatusMessage = string.Format("Import Process Completed With Errors");
			}
			else
				StatusMessage = string.Format("Import Process Completed");

			IsWindowEnabled = true;
		}

		#endregion

		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged;

		protected virtual void OnPropertyChanged(string propertyName)
		{
			var handler = PropertyChanged;
			if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
		}

		#endregion
	}
}
